--居合刃斩
local juherenzhan = fk.CreateSkill {
    name = "mcdh_juherenzhan",
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

local mcdh_kairen_num2str = function(num)
    if num == 0 then
        return 0
    elseif num == 1 then
        return "#mcdh_kairen_1"
    elseif num == 2 then
        return "#mcdh_kairen_2"
    elseif num == 3 then
        return "#mcdh_kairen_3"
    end
end

local mcdh_kairen_str2num = function(str)
    if str == 0 then
        return 0
    elseif str == "#mcdh_kairen_1" then
        return 1
    elseif str == "#mcdh_kairen_2" then
        return 2
    elseif str == "#mcdh_kairen_3" then
        return 3
    end
end

juherenzhan:addEffect(fk.CardUsing,{
    can_trigger = function(self, event, target, player, data)
        if event == fk.CardEffectCancelledOut then
            if data.card.trueName ~= "slash" or not player:hasSkill(self) then return false end
            if data.to == player.id then -- 当你使用闪抵消其他角色的杀时
                for _, card in ipairs(data.cardsResponded) do
                    if card.name == "jink" then
                        return true
                    end
                end
            end
        else
            return player == target and player:hasSkill(self) and data.card.color == Card.NoColor
        end
    end,
    on_cost = function (self, event, target, player, data)
        if event == fk.CardEffectCancelledOut then
            return player.room:askForSkillInvoke(player,self.name,nil,"#mcdh_juherenzhan:"..data.from)
        else
            return true
        end
    end,
    on_use = function (self, event, target, player, data)
        local room = player.room
        if event == fk.CardEffectCancelledOut then
            room:useVirtualCard("dismantlement",nil,player,data.from,self.name)
        else
            local num = mcdh_kairen_str2num(player:getMark("@mcdh_kairen"))
            num = num + 1
            room:setPlayerMark(player,"@mcdh_kairen",mcdh_kairen_num2str(num))
        end
    end
})

juherenzhan:addEffect(fk.CardEffectCancelledOut,{
    can_trigger = function(self, event, target, player, data)
        if event == fk.CardEffectCancelledOut then
            if data.card.trueName ~= "slash" or not player:hasSkill(self) then return false end
            if data.to == player.id then -- 当你使用闪抵消其他角色的杀时
                for _, card in ipairs(data.cardsResponded) do
                    if card.name == "jink" then
                        return true
                    end
                end
            end
        else
            return player == target and player:hasSkill(self) and data.card.color == Card.NoColor
        end
    end,
    on_cost = function (self, event, target, player, data)
        if event == fk.CardEffectCancelledOut then
            return player.room:askForSkillInvoke(player,self.name,nil,"#mcdh_juherenzhan:"..data.from)
        else
            return true
        end
    end,
    on_use = function (self, event, target, player, data)
        local room = player.room
        if event == fk.CardEffectCancelledOut then
            room:useVirtualCard("dismantlement",nil,player,data.from,self.name)
        else
            local num = mcdh_kairen_str2num(player:getMark("@mcdh_kairen"))
            num = num + 1
            room:setPlayerMark(player,"@mcdh_kairen",mcdh_kairen_num2str(num))
        end
    end
})

Fk:loadTranslationTable {

    ["mcdh_juherenzhan"] = "居合",--居合刃斩
    [":mcdh_juherenzhan"] = "当你使用【闪】抵消一名角色使用的【杀】时，你可以视为对其使用一张【过河拆桥】。当你使用无色牌时，你获得1枚「开刃」标记",
    ["#mcdh_juherenzhan"] = "居合：你可以视为对 %src 使用一张【过河拆桥】",
    ["@mcdh_kairen"] = "开刃",
    ["#mcdh_kairen_1"] = "1",
    ["#mcdh_kairen_2"] = "<font color='yellow'>2</font>",
    ["#mcdh_kairen_3"] = "<font color='red'>3</font>",
}

return juherenzhan